/*
* @(#)ListDmsSelectArchiveFolderAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.RootRetrievalManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;

/**
 * <p>Class Name:       ListDmsSelectArchiveFolderAction.java    </p>
 * <p>Description:      The list action class for ListDmsFolderSelect.jsp</p>
 *
 *    @author           anson.mo
 *    @company          DCIVision Limited
 *    @creation date    11/01/2006
 *    @version          $Revision: 1.4.2.1 $
 */

public class ListDmsSelectArchiveFolderAction extends AbstractListAction {
	
	public static final String REVISION = "$Revision: 1.4.2.1 $";

	public String getFunctionCode() {
		return (SystemFunctionConstant.DMS_ARCHIVE);
	}

	public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsDocumentDAObject");
	}
	
	public ListDmsSelectArchiveFolderAction(){
		super();
    this.setListName("folderList");
	}
	
	public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    ListDmsSelectArchiveFolderForm listForm = (ListDmsSelectArchiveFolderForm)form;
    SessionContainer sessionContainer = this.getSessionContainer(request);
		Connection conn = this.getConnection(request);
		
		DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    DmsDocument documentRoot = null;
    List folderTreeList = new ArrayList();
    List folderList = new ArrayList();
		
		String parentID = listForm.getParentID();
		String rootID = listForm.getRootID();		
		String actionType = listForm.getActionType();
		
		try{
			List publicRootList = rootRetrievalManager.getAccessiblePublicRootsList();
			request.setAttribute("publicRootList", publicRootList);
			
			if (Utility.isEmpty(parentID) || Utility.isEmpty(rootID) || "null".equals(parentID) || "null".equals(rootID)){
				if (!Utility.isEmpty(publicRootList)){
					DmsRoot folderRoot = (DmsRoot)publicRootList.get(0);
          DmsDocument folderRootDocument = docRetrievalManager.getRootFolderByRootID(folderRoot.getID());
          
          documentRoot = docRetrievalManager.getRootFolderByRootID(folderRootDocument.getRootID());
          request.setAttribute("rootFolder", documentRoot);

//          folderTreeList = docRetrievalManager.getOnlyFolderByParentIDRootID(folderRootDocument.getID(), folderRootDocument.getRootID());
//          request.setAttribute("folderTreeList", folderTreeList);
//          request.setAttribute(this.m_sListName,folderTreeList);
          
          listForm.setRootID(folderRootDocument.getRootID().toString());
          listForm.setParentID(folderRootDocument.getID().toString());
				}
			} else if (!Utility.isEmpty(parentID) && !Utility.isEmpty(rootID)){
				documentRoot = docRetrievalManager.getRootFolderByRootID(TextUtility.parseIntegerObj(listForm.getRootID()));
        request.setAttribute("rootFolder", documentRoot);

//        folderTreeList = docRetrievalManager.getOnlyFolderByParentIDRootID(documentRoot.getID(), TextUtility.parseIntegerObj(listForm.getRootID()));
//        request.setAttribute("folderTreeList", folderTreeList);
        
//        folderList = docRetrievalManager.getOnlyFolderByParentIDRootID(TextUtility.parseIntegerObj(listForm.getParentID()),TextUtility.parseIntegerObj(listForm.getRootID()));
//        request.setAttribute(this.m_sListName,folderList);
			}
			
//			if ("addFolder".equals(actionType)){
//				String[] basicIDs = listForm.getBasicSelectedID();
//				HttpSession session = request.getSession();
//				if (!Utility.isEmpty(session.getAttribute("archiveFolder"))){
//					String[] sesBasicIDs = (String[])session.getAttribute("archiveFolder");
//					String[] tmpBasicIDs = new String[basicIDs.length + sesBasicIDs.length];
//					int index = 0;
//					if (!Utility.isEmpty(sesBasicIDs)){
//						for (int i =0; i < sesBasicIDs.length; i++){
//							tmpBasicIDs[index] = sesBasicIDs[i];
//							++index;
//						}
//					}
//					if (!Utility.isEmpty(basicIDs)){
//						for (int i = 0; i < basicIDs.length; i++){
//							tmpBasicIDs[index] = basicIDs[i];
//							++index;
//						}
//					}
//					session.setAttribute("archiveFolder",tmpBasicIDs);
//				}else {
//					session.setAttribute("archiveFolder",basicIDs);
//				}	
//			}
      
      listForm.setDisplayOption("Z");
      super.getListData(mapping, listForm, request, response);
		} catch(ApplicationException ex){
      log.error(ex, ex);
		}
	}
}
